package com.koron.css2.newProjectCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.Enum.PjCollectionPaymentStatus;
import com.koron.css2.newProjectCenter.bean.PjCollection;
import com.koron.css2.newProjectCenter.bean.query.PjCollectionQuery;
import com.koron.css2.newProjectCenter.mapper.PjCollectionMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.bean.OrgUserQueryBean;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 工程报装-工程款明细-列表查询
 *
 * @author 朱超
 * @since 2022-01-19 10:44:39
 */
public class PjCollectionList implements ServerInterface {

    /**
     * 缴费方式  用于数据字典取值
     */
    private static final String PAYMENT_METHOD = "BPP";

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjCollectionMapper pjCollectionMapper = factory.getMapper(PjCollectionMapper.class);
            OrgUserMapper orgUserMapper = factory.getMapper(OrgUserMapper.class);
            // 1. 获取请求实例
            PjCollectionQuery pjCollectionQuery = JsonUtils.objectToPojo(req.getData(),PjCollectionQuery.class);
            // 2. 分页
            PageHelper.startPage(pjCollectionQuery.getPage(), pjCollectionQuery.getPageCount());
            // 3. 查询数据
            List<PjCollection> pjCollectionList = pjCollectionMapper.getList(pjCollectionQuery);
            //  获取缴费方式数据字典
            Map<String, String> paymentMethodMap = Tools.mapDicByCode(factory, PAYMENT_METHOD);
            // 查询出系统中的人员信息
            List<OrgUserBean> orgUserList = orgUserMapper.selectList(new OrgUserQueryBean());
            // 将人员信息转成Map
            Map<String, OrgUserBean> orgUserMap = orgUserList.stream().collect(Collectors.toMap(OrgUserBean::getAccount, Function.identity()));
            for (PjCollection pjCollection:pjCollectionList) {
                String statusName = PjCollectionPaymentStatus.getNameByStatus(pjCollection.getStatus());
                // 收款状态
                pjCollection.setStatusName(statusName);
                // 缴费渠道
                if (pjCollection.getPaymentMethod() != null && paymentMethodMap.containsKey(pjCollection.getPaymentMethod().toString())){
                    pjCollection.setPaymentMethodName(paymentMethodMap.get(pjCollection.getPaymentMethod().toString()));
                }
                // 收款员
                if (StringUtils.isNotEmpty(pjCollection.getPaymentStaff())){
                    pjCollection.setPaymentStaff(orgUserMap.get(pjCollection.getPaymentStaff()).getUserName());
                }
            }
            // 4. 返回数据
            MessageBean<PageInfo> message = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "工程款明细查询成功", PageInfo.class);
            message.setData(new PageInfo<>(pjCollectionList));

            return message;
        } catch (Exception e) {
            logger.error("工程款明细查询失败：",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"工程款明细查询失败",null);
        }
    }
}
